package com.example.democonfigserver.modules.app;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

/**
 * @author wangbin
 */
public interface AppRepository extends JpaRepository<App, Integer> {
    @Query(value = "select distinct a from App a left join fetch a.configList c left join fetch c.env e where a.project.id=:projectId",
            countQuery = "select count(a) from App a where a.project.id=:projectId")
    Page<App> findPageByProjectId(Pageable pageable, @Param("projectId") Integer projectId);

    @Query("select distinct a from App a left join fetch a.configList c where a.id=:id")
    Optional<App> findIdFetch(Integer id);

    @Query("select distinct a from App a " +
            "left join fetch a.configList c " +
            "left join fetch c.propertyList pr " +
            "where a.code=:appName and a.project.code=:projectName and c.env.code=:profile")
    List<App> findConfiguration(String projectName, String appName, String profile);

    @Query("select a from App a left join fetch a.configList c where c.id in :configIds")
    List<App> findFetchConfigByConfigId(List<Integer> configIds);
}
